package com.jason.quectel.activities;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.jason.quectel.R;
import com.jason.quectel.fragment.OneFragment;
import com.jason.quectel.fragment.ThreeFragment;
import com.jason.quectel.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class ViewPagerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        TabLayout tabLayout = findViewById(R.id.tablayout);
        ViewPager viewpager = findViewById(R.id.viewpager);
        //viewpager.setAdapter(new MyPagerAdapter(getData()));
        viewpager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), getFragments(), getData()));
        tabLayout.setupWithViewPager(viewpager);
    }

    private List<Fragment> getFragments() {
        ArrayList<Fragment> list = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());
        return list;
    }

    private List<String> getData() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add("item" + i);
        }
        return list;
    }

    public static class MyPagerAdapter extends PagerAdapter {
        private List<String> totallist = new ArrayList<>();

        public MyPagerAdapter(List<String> list) {
            this.totallist.clear();
            totallist.addAll(list);
        }

        @Override
        public int getCount() {
            return totallist.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_viewpager, null);
            TextView textView = (TextView) view.findViewById(R.id.tv_item_viewpager);
            textView.setText(totallist.get(position));
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(((View) object));
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return totallist.get(position);
        }
    }

    public static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> fragments;
        private List<String> titles;

        public MyFragmentPagerAdapter(@NonNull FragmentManager fm, List<Fragment> fragments, List<String> titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments == null ? 0 : fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles == null ? "" : titles.get(position);
        }
    }
}